/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GrupIslemleri.java
 *
 * Created on 26.Ara.2011, 00:47:24
 */
package oopdesignpatternsproject;


import commands.GroupCommander;
import commands.RemoveGroupCommand;
import commands.AddGroupCommand;
import entityclasses.Session;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultTreeSelectionModel;

import entityclasses.Group;


/**
 *
 * @author umutcan
 */
public class GrupIslemleri extends javax.swing.JFrame {
    
    /** Creates new form GrupIslemleri */
    public GrupIslemleri() {
        initComponents();
        
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        
        DefaultTreeSelectionModel  dtsm = new DefaultTreeSelectionModel();
        dtsm.setSelectionMode(DefaultTreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
        treeGruplar.setSelectionModel(dtsm);
        DefaultTreeModel dtm = (DefaultTreeModel)treeGruplar.getModel();
        dtm.setRoot(new DefaultMutableTreeNode("Arkadaş Grupları"));
        
        //((DefaultMutableTreeNode)dtm.getRoot()).add(new DefaultMutableTreeNode(Session.currentUser.getProfile().getFriends()));
        
        for (Group g : Session.currentUser.getProfile().getGroups())
        { 
            DefaultMutableTreeNode newNode;
            newNode = new DefaultMutableTreeNode(g);
            ((DefaultMutableTreeNode)dtm.getRoot()).add(newNode);
            traverse(g, newNode);
        }
              
        
                
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        treeGruplar = new javax.swing.JTree();
        txtAd = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btnEkle = new javax.swing.JButton();
        btnSil = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        jScrollPane1.setName("jScrollPane1"); // NOI18N

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
        treeGruplar.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        treeGruplar.setName("treeGruplar"); // NOI18N
        jScrollPane1.setViewportView(treeGruplar);

        txtAd.setName("txtAd"); // NOI18N

        jLabel1.setText("Adı");
        jLabel1.setName("jLabel1"); // NOI18N

        btnEkle.setText("Ekle");
        btnEkle.setName("btnEkle"); // NOI18N
        btnEkle.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnEkleMouseClicked(evt);
            }
        });

        btnSil.setText("Sil");
        btnSil.setName("btnSil"); // NOI18N
        btnSil.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnSilMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(btnEkle)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnSil))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addGap(45, 45, 45)
                            .addComponent(txtAd, javax.swing.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtAd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEkle)
                    .addComponent(btnSil))
                .addContainerGap(11, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnEkleMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnEkleMouseClicked
    
     GroupCommander gc = new GroupCommander(treeGruplar);
     gc.setAddGroupCommand(new AddGroupCommand(txtAd.getText()));
     gc.add();

        
        
    }//GEN-LAST:event_btnEkleMouseClicked

    private void btnSilMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnSilMouseClicked
        
     GroupCommander gc = new GroupCommander(treeGruplar);
     gc.setRemoveGroupCommand(new RemoveGroupCommand());
     gc.remove();
        
        
        
    }//GEN-LAST:event_btnSilMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GrupIslemleri.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GrupIslemleri.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GrupIslemleri.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GrupIslemleri.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GrupIslemleri().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEkle;
    private javax.swing.JButton btnSil;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree treeGruplar;
    private javax.swing.JTextField txtAd;
    // End of variables declaration//GEN-END:variables

    private void traverse(Group g, DefaultMutableTreeNode localRoot) {
        
        if (!g.getSubgroups().isEmpty())
        {
            for(Group g2 : g.getSubgroups())
            {
                DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(g2);
                localRoot.add(newNode);
                traverse(g2, newNode);
            }
        }
    }
}
